<html><head><title>//透明的JPanel</title></head><body><div id='tit'>//透明的JPanel</div><div id='cate'>源码品读</div><div id='date'>2009年09月05日 星期六 03:00 P.M.</div><div id='page'>32</div><a id='url' href='http://hi.baidu.com/hxzon/blog/item/64697f314b3e4110eac4af44.html'>http://hi.baidu.com/hxzon/blog/item/64697f314b3e4110eac4af44.html</a><div id='cnt'><div>
 //透明的JPanel
 <br /> 
 <br /> package com.xxx.goodcode;
</div> 
<div> 
</div> 
<div>
 import java.awt.AlphaComposite;
 <br /> import java.awt.Color;
 <br /> import java.awt.Graphics;
 <br /> import java.awt.Graphics2D;
 <br /> import java.awt.RenderingHints;
 <br /> import java.awt.image.BufferedImage;
</div> 
<div> 
</div> 
<div>
 import javax.swing.JFrame;
 <br /> import javax.swing.JPanel;
</div> 
<div> 
</div> 
<div>
 public class TranslucentPanel extends JPanel {
 <br /> BufferedImage img;
 <br /> public TranslucentPanel() {
 <br /> &nbsp;&nbsp; setOpaque(false);
 <br /> }
</div> 
<div> 
</div> 
<div>
 public void paintComponent(Graphics g) {
 <br /> &nbsp;&nbsp; img = new BufferedImage(getWidth(), getHeight(),
 <br /> &nbsp;&nbsp;&nbsp;&nbsp; BufferedImage.TYPE_INT_ARGB);
 <br /> &nbsp;&nbsp; Graphics2D g2d = img.createGraphics();
 <br /> &nbsp;&nbsp; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 <br /> &nbsp;&nbsp;&nbsp;&nbsp; RenderingHints.VALUE_ANTIALIAS_ON);
 <br /> &nbsp;&nbsp; g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
 <br /> &nbsp;&nbsp;&nbsp;&nbsp; 0.75f));// 设置合成规则
 <br /> &nbsp;&nbsp; g2d.setColor(Color.BLACK);// 设置背景色
 <br /> &nbsp;&nbsp; g2d.fillRect(0, 0, getWidth(), getHeight());
 <br /> &nbsp;&nbsp; g.drawImage(img, 0, 0, null);
 <br /> }
</div> 
<div> 
</div> 
<div> 
 <p>public static void main(String args[]) {<br /> &nbsp;&nbsp; JFrame f = new JFrame(&quot;translucent test panel&quot;);<br /> &nbsp;&nbsp; f.add(new TranslucentPanel());<br /> &nbsp;&nbsp; f.setSize(400, 300);<br /> &nbsp;&nbsp; f.setVisible(true);<br /> }<br /> }<br /> //hxzon:由于paintComponent()调用频繁，img不断的创建，并且g2d,g相当于每次都绘制两遍，性能受影响。全局img保存着JPanel的图形，可以做其他用途，例如在图形外被使用，从而绘制JPanel的局部区域。</p> 
 <p>==========================<br /> <br /> <br /> public class TranslucentPanel extends JPanel {<br /> BufferedImage image = null;<br /> @Override<br /> public void paint(Graphics g) {<br /> &nbsp;&nbsp;&nbsp; if (image == null ||<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; image.getWidth() != getWidth() ||<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; image.getHeight() != getHeight()) {<br /> continued<br /> ONLINE<br /> DEMO250 CHAPTER 11 REPAINT MANAGER<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; image = (BufferedImage) createImage(getWidth(), <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; getHeight());<br /> &nbsp;&nbsp;&nbsp; }<br /> &nbsp;&nbsp;&nbsp; Graphics2D g2 = image.createGraphics();<br /> &nbsp;&nbsp;&nbsp; g2.setClip(g.getClip());<br /> &nbsp;&nbsp;&nbsp; super.paint(g2);<br /> &nbsp;&nbsp;&nbsp; g2.dispose();<br /> &nbsp;&nbsp;&nbsp; g2 = (Graphics2D) g.create();<br /> &nbsp;&nbsp;&nbsp; g2.setComposite(AlphaComposite.SrcOver.derive(0.2f));<br /> &nbsp;&nbsp;&nbsp; g2.drawImage(image, 0, 0, null);<br /> }<br /> }</p> 
</div></div></body></html>